package com.daigoudao.web.action;

import java.io.File;
import java.util.Date;

import com.daigoudao.config.ConfigManager;
import com.daigoudao.util.Constants;
import com.daigoudao.util.FileUtil;
import com.daigoudao.util.StringUtils;
import com.daigoudao.web.common.BaseAction;

public class UploadAction extends BaseAction {

	private File image;

	private String imageFileName;

	private String imageContentType;

	private String urlPath;

	public String upload() {
		if (image != null) {
			String root = ConfigManager.getConfigString("upload.resDir");
			String path = StringUtils.formatDate(new Date(), "yyyy-MM-dd");
			String basePath = root + File.separator + path;
			FileUtil.createDir(basePath);
			String fileName = imageFileName;
			String extName = FileUtil.getExtention(fileName);

			fileName = StringUtils.getUUID() + extName;
			File destFile = new File(basePath, fileName);

			// 文件复制
			try {
				FileUtil.copy(image, destFile);
			} catch (Exception e) {
				ajaxMsg = Constants.ERR_MSG;
			}
			// 文件在数据库存储
			urlPath = File.separator + path + File.separator + fileName;
		}
		return SUCCESS;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageContentType() {
		return imageContentType;
	}

	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}
	
	public void setUrlPath(String urlPath) {
		this.urlPath = urlPath;
	}

	public String getUrlPath() {
		return urlPath;
	}

}
